class CreditCardV < ActiveMerchant::Billing::CreditCard
  CC_OUTSIDE = ZepFrog::Config[:credit_card_outside]  
  CC_VALIDATE = (ZepFrog::Config[:credit_card_validate_transaction] || false)   #  !!! finish and set true
  
  @options = Hash.new
  @add_options = Hash.new
  @out_options = Hash.new
  
  attr_reader :out_options
  
  def initialize(options ={})
    super options[:main]
    @options = (options[:main].merge (options[:add_options] || {:empty =>""}))
    @add_options = options[:add_options]
    @options[:user_profile] = (@add_options[:user_id].to_s || "") + "__" + (@options[:last_name] || "")
    @options[:zipcode] = "07306" #temporary
  end
  
  def store_credit_card
    # validate
    if self.valid?
      begin
        self.customer_profile    
        self.customer_payment_profile
      rescue ZepFrog::PaymentError => e
        # if @customer_profile
        self.delete_customer_profile
        # end
        # 
        # if @customer_payment_profile
        #   self.delete_customer_payment_profile
        # end
        raise ZepFrog::PaymentError, e.inspect
      end
      #  return false to sele ct all errors
    end    
  end
  
  # think add other output params
  def out_options
    @out_options = {:customer_profile_id => @options[:customer_profile_id]}
    @out_options[:customer_payment_profile_id] = @options[:customer_payment_profile_id]
    @out_options
  end
  
  def customer_profile
    
    @customer_profile = self.get_customer_profile || ACCOUNT_GATEWAY.create_customer_profile({:profile => {:email => @options[:user_profile]}})
    
    unless @customer_profile.success?
      self.errors.add_to_base @customer_profile.message
      raise ZepFrog::PaymentError, @customer_profile.message
    end
    @options[:customer_profile_id] = @customer_profile.authorization
  end
  
  def get_customer_profile
    if @customer_profile.nil? && @options[:customer_profile_id]
      return ACCOUNT_GATEWAY.get_customer_profile({:customer_profile_id => @options[:customer_profile_id]})
    end
  end
  
  def customer_payment_profile
    # assert_equal @customer_payment_profile_id, response.params['customer_payment_profile_id']
    if @customer_profile
      puts @options[:customer_profile_id].to_s + "options[:customer_profile_id]"
      @customer_payment_profile = ACCOUNT_GATEWAY.create_customer_payment_profile(
                                      :customer_profile_id => @options[:customer_profile_id],
                                      :payment_profile => {
                                        :payment => {:credit_card => self},
                                        # add bill_to and ship_to later
                                        :bill_to => {:zip => @options[:zipcode]}
                                  })
      # real validation using transaction 0.01
      if CC_VALIDATE
        self.validate_customer_payment_profile
      end
      unless @customer_payment_profile.params['customer_payment_profile_id']
        # delete customer_profile later
        self.errors.add_to_base @customer_payment_profile.message
        raise ZepFrog::PaymentError, @customer_payment_profile.message
      end
      @options[:customer_payment_profile_id] = @customer_payment_profile.params['customer_payment_profile_id']
    end

  end
  
  def self.find_card(options = {})
    out_options = Hash.new
    custom_profile
    custom_payment_profile
    if options[:customer_profile_id]
      custom_profile = ACCOUNT_GATEWAY.get_customer_profile({:customer_profile_id => options[:customer_profile_id]})
      
      #several account re-write !!!!!
      if custom_profile.success?
        custom_payment_profile = ACCOUNT_GATEWAY.get_customer_payment_profile({
                                        :customer_profile_id => options[:customer_profile_id],
                                        :customer_payment_profile_id => (options[:customer_payment_profile_id] || custom_profile[:payment_profile][:customer_payment_profile_id])
                                        })
        if custom_payment_profile.success?
          out_options = { :customer_profile_id => customer_profile.authorization,
                          :customer_profile => customer_profile,
                          :customer_payment_profile_id => (options[:customer_payment_profile_id] || custom_profile[:payment_profile][:customer_payment_profile_id]),
                          :customer_payment_profile => customer_payment_profile
                        }
        else
          raise ZepFrog::PaymentError, custom_profile.message
        end
      else
        raise ZepFrog::PaymentError, customer_payment_profile.message
      end
    end
    out_options
  end
  
  def update_card
  end
  
  def update_customer_profile
  end
  
  def update_customer_payment_profile
  end
  
  def self.delete_card(options={})
    if options[:customer_profile_id]
      response = ACCOUNT_GATEWAY.delete_customer_profile({
                        :customer_profile_id => (options[:customer_profile_id])})
      unless response.success?
        self.errors.add_to_base response.message
        raise ZepFrog::PaymentError, response.message
      end
    end
  end
  
  def delete_customer_profile
    if @customer_profile || @options[:customer_profile_id]
      ACCOUNT_GATEWAY.delete_customer_profile({
                        :customer_profile_id => (@customer_profile.authorization || @options[:customer_profile_id])})
    end
  end
  
  def delete_customer_payment_profile
  end
  
  def validate_customer_payment_profile
    ACCOUNT_GATEWAY.validate_customer_payment_profile(
                            :customer_profile_id => @options[:customer_profile_id],
                            :customer_payment_profile_id => @options[:customer_payment_profile_id],
                            :customer_address_id => nil, # do later validation for address,
                            :validation_mode => :live # for test mode -  :test :  :validation_mode — :live or :test In Test Mode, only field validation is performed. In Live Mode, a transaction is generated and submitted to the processor with the amount of $0.01. If successful, the transaction is immediately voided. (REQUIRED)
                            )
  end

  # def validate_card
  #    unless self.valid?
  #      
  #    #      unless @credit_card.valid?
  #    #        @credit_card.errors.full_messages.each do |message|
  #    #          errors.add_to_base message
  #    #        end
  #    #      end
  #  end
  #  # validate_customer_payment_profile

end
